<?php

namespace app\admin\controller\wechat;


use app\admin\controller\Base;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
use app\common\model\Wechat as WechatModel;
use helper\Wechat as WechatClass;
use EasyWeChat\Factory;


class Keys extends Base
{
    /**
     * 消息类型
     * @var array
     */
    public $types = [
        'text'  => '文字', 
        'news' => '图文', 
        'image' => '图片', 
        // 'music' => '音乐',
        // 'video' => '视频', 
        // 'voice' => '语音', 
        // 'customservice' => '转客服',
    ];
	public function initialize()
    {
        parent::initialize(); 
        $map[] = ['status','=',1];
        $wx = WechatModel::where($map)->find();
        $this->Wechat = new WechatClass($wx['domain']);
        $this->app = Factory::officialAccount($this->Wechat->get_config());
        
        $this->wechatConfig = WechatModel::loadList([],100);
        View::assign('msgTypes',$this->types);
        View::assign('wechat',$this->wechatConfig['list']);
    }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if (request()->isPost()) {
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $limit = $this->request->post('limit') ? $this->request->post('limit') : 20;
            $where = []; 
            $list = Db::name('wx_keys')->where($where)->order('id desc')->paginate($limit)->each(function($item,$key){
                $item['msgtype'] = $this->types[$item['msgtype']];
                return $item;
            })->toArray();
            
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data'], 'count' => $list['total']];
        }
        
        return view();
    }
    
    public function state(){
        $data = $this->request->post();
        $id = $this->request->post('id');
        if($id){
            $info = Db::name('wx_keys')->find($id);
            $field = $data['field'];
            $status = $info[$field]==1?0:1;
            Db::name('wx_keys')->where('id',$id)->update(['status'=>$status]);

            $this->success(lang('edit success'));

        }else{
            $this->error(lang('invalid data'));
        }
    }
    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        if (Request::isPost()) {
            $data = $this->request->post();
            $res = Db::name('wx_keys')->insert($data);
            if ($res) {
                $this->success('添加成功',url('index'));
            }else{
                $this->error('添加失败');
            }
        }else{
            $view = [
                'info' => [],
            ];
            View::assign($view);
            return view();
        }
    }


    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit()
    {
        if (Request::isPost()) {
            $data = $this->request->post();
            // print_r($data);die;
            $res = Db::name('wx_keys')->where('id',$data['id'])->update($data);
            if ($res !== false) {
                $this->success('修改成功',url('index'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $id = $this->request->get('id');
            $info = Db::name('wx_keys')->where('id',$id)->find();
            $view = [
                'info' => $info,
            ];
            View::assign($view);
            return view('add');
        }
    }
    public function del()
    {
        $id = $this->request->get('id');
        $res = Db::name('wx_keys')->where('id',$id)->delete();
        if ($res) {
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }
}
